with Ada.Text_Io; use Ada.Text_Io;
with Chaines.Variables; use Chaines.Variables;

package body Chaines.Variables is
   function Chainer(S: String) return Chaine_Variable is
      Ptr : Chaine_Variable := new String(S'First .. S'Last);
   begin
      Ptr.all := S;
      return Ptr;
   end Chainer;

   function Chainer(P :Chaine_Variable) return Chaine_Variable is
      Ptr: Chaine_Variable := new String(P.all'First..P.all'last);
   begin
      Ptr.all := P.all;
      return Ptr;
   end Chainer;

   function Dechainer(P: Chaine_Variable) return String is
   begin
      return P.all;
   end Dechainer;

   function Lire_Ligne return Chaine_Variable is
      function Aux (S : String) return String is
      begin
         if End_Of_Line then
            return S;
         else
            declare
               C: Character;
            begin
               Get(C);
               return Aux(Concat(S,(1 => C)));
            end;
         end if;
      end ;
   begin
      return Chainer(Aux(""));
   end Lire_Ligne;

   procedure Ecrire(C: Chaine_Variable) is
   begin
      Put(C.all);
   end ecrire;

   function Concat(C1,C2:Chaine_Variable) return Chaine_Variable is
      Res: String(1..C1.all'Length+C2.all'Length);
   begin
      Res(Res'First..C1.all'Length) = C1.all;
      Res(C1.all'Length+1..Res'Last) = C2.all;
      return Chainer(Res);
   end Concat;

   function Tri(C: Chaine_Variable) return Chaine_Variable is
      Ptr : Chaine_Variable;
   begin
      ptr := Chainer(Tri(C.all));
      return Ptr;
   end Tri;

   function Equal(C1,C2 : Chaine_Variable) return Boolean is
   begin
      return C1.all=C2.all;
   end Equal;


end Chaines.Variables;
